package org.lee.main;

import org.lee.Teacher;
import org.lee.bean.School;
import org.lee.bean.Student;
import org.lee.config.StudentConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * @author lizhe
 */
@Component
public class TestDI {

    @Autowired
    private Student tom;

    @Resource
    private Student lily;

    @Autowired
    @Qualifier("xiaoming")
    private Student xiaoming;

    @Autowired
    private StudentConfiguration configuration;

    @Autowired
    private School school;

    public static void main(String[] args) {

        AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext(StudentConfiguration.class);
        String[] beanDefinitionNames = configApplicationContext.getBeanDefinitionNames();
        System.out.println(Arrays.toString(beanDefinitionNames));

        //Configuration注入
        Student xiaoli = configApplicationContext.getBean("xiaoli", Student.class);
        System.out.println(xiaoli);


        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student jack = (Student) context.getBean("jack");
        System.out.println(jack);

        TestDI testDI = context.getBean(TestDI.class);
        testDI.referTom();

        System.out.println(testDI.lily);

        System.out.println(testDI.xiaoming);


        System.out.println(testDI.school);

        Teacher teacher = context.getBean(Teacher.class);
        System.out.println(teacher);


    }

    private void referTom() {
        System.out.println(tom);
    }


}
